package cc.hexie.fish.service;

import cc.hexie.fish.dao.AccountDao;
import cc.hexie.fish.pojo.AccountBean;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AccountServiceImpl implements AccountService {

    private Log logger = LogFactory.getLog(this.getClass());

    @Autowired
    AccountDao accountDao;

    @Override
    public void getAllAccount() {
        List<AccountBean> beanList = accountDao.findAll();
        for (AccountBean bean: beanList) {
            logger.info(bean.toString());
        }
    }

    @Override
    public AccountBean getAccountById(int id) {
        AccountBean bean = accountDao.findById(id);
        logger.info(bean.toString());
        return bean;
    }

    @Override
    public void transMoney(AccountBean fromAccount, AccountBean toAccount) {
        fromAccount.setMoney(fromAccount.getMoney() - 100);
        toAccount.setMoney(toAccount.getMoney() + 100);
        logger.info(fromAccount.toString());
        logger.info(toAccount.toString());

        accountDao.updateMoney(fromAccount);
        accountDao.updateMoney(toAccount);

        logger.info("trans money finished...");
    }
}
